package com.cdream.basic.algorithm.leetcode.array;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cdream
 * @date 11/30/21
 */
public class Question400 {
    public static void main(String[] args) {
        System.out.println(new Question400().findNthDigit(10));
    }

    // todo
    public int findNthDigit(int n) {
        // 9 * 1 + 90 * 2 + 900 * 3 + ...
        int i = 1;
        while (n > i * 9 * Math.pow(10, i - 1)) {
            n -= i * 9 * Math.pow(10, i - 1);
            i++;
        }
        // 第几个数
        int cixu = n / i;
        if (n % i != 0) {
            cixu += 1;
        }
        String s = Integer.toString((int) Math.pow(10, i - 1) + cixu - 1);
        //10 +
        System.out.println(s);
        // 数里面的第几位
        // 4 9 -> 3 8 -> 2
        // 9 / 4 = 1 -> 0
        // 10 / 4 = 2; -> 1
        // 11 / 4 = 3; -> 2
        // 12 / 4 = 0; -> 3
        int mod = n % i - 1;
        if (mod < 0) {
            mod += i;
        }

        return s.charAt(mod) - '0';
    }

    public String[] findRelativeRanks(int[] score) {
        int[] copy = Arrays.copyOf(score, score.length);
        int l = score.length;
        Arrays.sort(copy);
        Map<Integer, String> map = new HashMap<>();
        for (int i1 = 0; i1 < copy.length; i1++) {
            int i = copy[i1];
            String value = Integer.toString(l - i1);
            if (value.equals("1")) {
                value = "Gold Medal";
            } else if (value.equals("2")) {
                value = "Silver Medal";
            } else if (value.equals("3")) {
                value = "Bronze Medal";
            }
            map.put(i, value);
        }

        String[] res = new String[score.length];
        for (int i = 0; i < score.length; i++) {
            res[i] = map.get(score[i]);
        }
        return res;
    }
}
